package plus.lgx.textsearcher.searcher.handler;

import lombok.SneakyThrows;
import plus.lgx.textsearcher.config.SearcherConfig;
import plus.lgx.textsearcher.entity.FileWrapper;
import plus.lgx.textsearcher.entity.PrintData;
import plus.lgx.textsearcher.searcher.printer.OutputHandler;

import java.io.File;
import java.io.FileReader;
import java.util.concurrent.atomic.AtomicInteger;

import static plus.lgx.textsearcher.global.Global.buffer;

/**
 * Class name: AbstractFileHandler
 *
 * Create time: 12/14/22 11:10 PM
 *
 * @author lgx
 * @version 1.0
 */
public abstract class AbstractFileHandler {

    final SearcherConfig config;

    final OutputHandler handler;

    final PrintData data;

    public AbstractFileHandler(SearcherConfig config, OutputHandler handler, PrintData data) {
        this.config = config;
        this.handler = handler;
        this.data = data;
    }

    @SneakyThrows
    String getContent(FileWrapper wrapper) {
        FileReader fr = new FileReader(wrapper.file);
        int read = fr.read(buffer, 0, config.bufferSize);
        fr.close();
        if (read < 0) {
            return null;
        }
        return new String(buffer, 0, read);
    }

    /**
     * 处理一个文件
     *
     * @param wrapper 文件包装实体
     */
    public abstract void handle(FileWrapper wrapper);

}
